package com.jiang.test;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadPoolInvokeAllTest2 {

	public static void main(String[] args) {
		
		Set<Callable<String>> alls = new HashSet<>();
		ExecutorService es = Executors.newFixedThreadPool(3);
		for(int i=0;i<10;i++) {
			int temp = i;
			alls.add(()->{
				return "线程名称"+Thread.currentThread().getName()+"|"+temp;
			});
		}
		// 批量线程执行
		try {
			List<Future<String>> futures = es.invokeAll(alls);
			System.out.println(futures.size());
			// 执行完毕后，返回打印
			for(Future<String> f: futures) {
				System.out.println(f.get());
			}
		} catch (InterruptedException | ExecutionException e1) {
			e1.printStackTrace();
		} 
			es.shutdown();
	}

}
